// platforminfo.cpp
#include "platforminfo.h"
#include <QSysInfo>
#include <QStringList>
#include <QDebug>

PlatformInfo::PlatformInfo(QObject *parent)
    : QObject(parent)
{
    detectPlatform();
}

void PlatformInfo::detectPlatform()
{
    // Qt版本信息
    m_qtVersion = QString("Qt %1.%2.%3")
                      .arg(QT_VERSION_MAJOR)
                      .arg(QT_VERSION_MINOR)
                      .arg(QT_VERSION_PATCH);

    // 构建ABI信息
    m_buildAbi = QSysInfo::buildAbi();

    // 内核版本
    m_kernelVersion = QSysInfo::kernelVersion();
    
    // 内核类型
    QString kernelType = QSysInfo::kernelType();

    // CPU架构
    m_cpuArchitecture = QSysInfo::currentCpuArchitecture();

    // 产品类型和名称
    m_productType = QSysInfo::productType();
    m_prettyProductName = QSysInfo::prettyProductName();

    // 调试输出 - 先输出原始值
    qDebug() << "=== 平台信息检测（原始值）===";
    qDebug() << "productType:" << m_productType;
    qDebug() << "prettyProductName:" << m_prettyProductName;
    qDebug() << "kernelType:" << kernelType;
    qDebug() << "kernelVersion:" << m_kernelVersion;
    qDebug() << "buildAbi:" << m_buildAbi;
    qDebug() << "currentCpuArchitecture:" << m_cpuArchitecture;

    // 检测平台类型 - 优先使用宏定义
#ifdef Q_OS_HARMONYOS
    m_platformType = "HarmonyOS";
    m_platformName = "HarmonyOS (鸿蒙)";
#elif defined(Q_OS_ANDROID)
    m_platformType = "Android";
    m_platformName = "Android";
#elif defined(Q_OS_IOS)
    m_platformType = "iOS";
    m_platformName = "iOS";
#elif defined(Q_OS_MACOS)
    m_platformType = "macOS";
    m_platformName = "macOS";
#elif defined(Q_OS_WIN)
    m_platformType = "Windows";
    m_platformName = "Windows";
#elif defined(Q_OS_LINUX)
    // Linux 平台需要进一步判断是否是 HarmonyOS
    // HarmonyOS 基于 Linux 内核，可能被识别为 Linux
    QString osType = m_productType.toLower();
    QString productName = m_prettyProductName.toLower();
    QString kernelTypeLower = kernelType.toLower();
    
    // HarmonyOS 检测：检查多个特征
    bool isHarmonyOS = false;
    
    // 方法1: 检查产品类型和名称
    if (osType.contains("harmonyos") || osType.contains("openharmony") || 
        productName.contains("harmonyos") || productName.contains("openharmony") ||
        productName.contains("鸿蒙")) {
        isHarmonyOS = true;
        qDebug() << "检测到 HarmonyOS（通过产品类型/名称）";
    }
    
    // 方法2: 检查 buildAbi（HarmonyOS 特定的 ABI）
    if (m_buildAbi.contains("harmonyos", Qt::CaseInsensitive) || 
        m_buildAbi.contains("openharmony", Qt::CaseInsensitive)) {
        isHarmonyOS = true;
        qDebug() << "检测到 HarmonyOS（通过 buildAbi）";
    }
    
    // 方法3: 检查内核版本特征（HarmonyOS 特定的内核版本格式）
    // HarmonyOS 内核版本可能包含特定标识
    
    if (isHarmonyOS) {
        m_platformType = "HarmonyOS";
        m_platformName = "HarmonyOS (鸿蒙)";
    } else {
        m_platformType = "Linux";
        m_platformName = m_prettyProductName.isEmpty() ? "Linux" : m_prettyProductName;
    }
#else
    // 通用检测 - 通过产品类型判断
    QString osType = m_productType.toLower();
    QString productName = m_prettyProductName.toLower();
    
    // HarmonyOS 检测（最优先）- 使用大小写不敏感匹配
    if (osType.contains("harmonyos", Qt::CaseInsensitive) || 
        osType.contains("openharmony", Qt::CaseInsensitive) || 
        productName.contains("harmonyos", Qt::CaseInsensitive) || 
        productName.contains("openharmony", Qt::CaseInsensitive) ||
        productName.contains("鸿蒙") ||
        m_buildAbi.contains("harmonyos", Qt::CaseInsensitive) || 
        m_buildAbi.contains("openharmony", Qt::CaseInsensitive)) {
        m_platformType = "HarmonyOS";
        m_platformName = "HarmonyOS (鸿蒙)";
        qDebug() << "检测到 HarmonyOS（通用检测）";
    }
    else if (osType == "android") {
        m_platformType = "Android";
        m_platformName = "Android";
    }
    else if (osType == "ios") {
        m_platformType = "iOS";
        m_platformName = "iOS";
    }
    else if (osType == "macos") {
        m_platformType = "macOS";
        m_platformName = "macOS";
    }
    else if (osType == "windows") {
        m_platformType = "Windows";
        m_platformName = "Windows";
    }
    else if (osType == "linux") {
        m_platformType = "Linux";
        m_platformName = m_prettyProductName.isEmpty() ? "Linux" : m_prettyProductName;
    }
    else {
        // 默认使用产品类型
        m_platformType = m_productType.isEmpty() ? "Unknown" : m_productType;
        m_platformName = m_prettyProductName.isEmpty() ? "Unknown Platform" : m_prettyProductName;
    }
#endif

    // 调试输出 - 最终结果
    qDebug() << "=== 平台信息检测（最终结果）===";
    qDebug() << "平台类型:" << m_platformType;
    qDebug() << "平台名称:" << m_platformName;
    qDebug() << "Qt版本:" << m_qtVersion;
    qDebug() << "构建ABI:" << m_buildAbi;
    qDebug() << "内核版本:" << m_kernelVersion;
    qDebug() << "CPU架构:" << m_cpuArchitecture;
    qDebug() << "产品类型:" << m_productType;
    qDebug() << "产品名称:" << m_prettyProductName;
}

